package com.ruoyi.project.rest.order.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.rest.detail.domain.RestOrderDetail;
import com.ruoyi.project.rest.detail.service.IRestOrderDetailService;
import com.ruoyi.project.rest.goods.domain.RestGoods;
import com.ruoyi.project.rest.goods.service.IRestGoodsService;
import com.ruoyi.project.rest.order.domain.OrderPay;
import com.ruoyi.project.rest.order.domain.RestOrderDto;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.rest.order.domain.RestOrder;
import com.ruoyi.project.rest.order.service.IRestOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2022-03-28
 */
@Controller
@RequestMapping("/rest/order")
public class RestOrderController extends BaseController {
    private String prefix = "rest/order";

    @Autowired
    private IRestOrderService restOrderService;
    @Autowired
    private IRestGoodsService restGoodsService;
    @Autowired
    private IRestOrderDetailService restOrderDetailService;

    @RequiresPermissions("rest:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    @GetMapping("/addOrder")
    public String addOrder() {
        return prefix + "/addOrder";
    }

    @GetMapping("/info/{orderNo}")
    public String info(@PathVariable("orderNo") String orderNo, ModelMap mmap) {
        RestOrder restOrder = restOrderService.selectRestOrderByOrderNo(orderNo);
        mmap.put("restOrder", restOrder);
        mmap.put("orderCreateTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, restOrder.getCreateTime()));
        RestOrderDetail orderDetail = new RestOrderDetail();
        orderDetail.setOrderNo(orderNo);
        List<RestOrderDetail> orderDetails = restOrderDetailService.selectRestOrderDetailList(orderDetail);
        mmap.put("orderGoodsList", orderDetails);
        return prefix + "/info";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("rest:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RestOrder restOrder) {
        startPage();
        List<RestOrder> list = restOrderService.selectRestOrderList(restOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("rest:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RestOrder restOrder) {
        List<RestOrder> list = restOrderService.selectRestOrderList(restOrder);
        ExcelUtil<RestOrder> util = new ExcelUtil<RestOrder>(RestOrder.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        RestGoods goods = new RestGoods();
        goods.setState(0);
        List<RestGoods> goodsList = restGoodsService.selectRestGoodsList(goods);
        mmap.put("goodsList", goodsList);
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("rest:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RestOrderDto restOrder) {
        return toAjax(restOrderService.save(restOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("rest:order:edit")
    @GetMapping("/edit/{orderNo}")
    public String edit(@PathVariable("orderNo") String orderNo, ModelMap mmap) {
        RestOrder restOrder = restOrderService.selectRestOrderByOrderNo(orderNo);
        mmap.put("restOrder", restOrder);
        RestOrderDetail orderDetail = new RestOrderDetail();
        orderDetail.setOrderNo(orderNo);
        List<RestOrderDetail> orderDetails = restOrderDetailService.selectRestOrderDetailList(orderDetail);
        mmap.put("orderGoodsList", orderDetails);
        RestGoods goods = new RestGoods();
        goods.setState(0);
        List<RestGoods> goodsList = restGoodsService.selectRestGoodsList(goods);
        mmap.put("goodsList", goodsList);
        return prefix + "/edit";
    }

    /**
     * 修改订单
     */
    //@RequiresPermissions("rest:order:edit")
    @GetMapping("/settlement/{orderNo}")
    public String settlement(@PathVariable("orderNo") String orderNo, ModelMap mmap) {
        RestOrder restOrder = restOrderService.selectRestOrderByOrderNo(orderNo);
        mmap.put("restOrder", restOrder);
        mmap.put("orderCreateTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, restOrder.getCreateTime()));
        RestOrderDetail orderDetail = new RestOrderDetail();
        orderDetail.setOrderNo(orderNo);
        List<RestOrderDetail> orderDetails = restOrderDetailService.selectRestOrderDetailList(orderDetail);
        mmap.put("orderGoodsList", orderDetails);
        return prefix + "/settlement";
    }


    /**
     * 修改保存订单
     */
    @RequiresPermissions("rest:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RestOrder restOrder) {
        return toAjax(restOrderService.updateRestOrder(restOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("rest:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(restOrderService.deleteRestOrderByOrderNos(ids));
    }

    @Log(title = "订单支付", businessType = BusinessType.OTHER)
    @PostMapping("/orderPay")
    @ResponseBody
    public AjaxResult orderPay(OrderPay orderPay) {
        return restOrderService.orderPay(orderPay);
    }
}
